package cn.schoolwow.util.module.query.instanceList.flow.get;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instance.QueryInstance;
import cn.schoolwow.util.module.query.instanceList.domain.InstanceListQueryOption;

import java.util.ArrayList;
import java.util.List;

public class GetInstanceMultipleFieldValueListBusiness implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        InstanceListQueryOption instanceQueryOption = flowContext.checkInstanceData(InstanceListQueryOption.class);
        List filterInstanceList = (List) flowContext.getData("filterInstanceList");
        String[] fieldNames = (String[]) flowContext.checkData("fieldNames");

        if(null==filterInstanceList){
            return;
        }
        List fieldValueList = new ArrayList();
        for(Object filterInstance:filterInstanceList){
            Object fieldValue = QueryInstance.newQuery(filterInstance).execute().getMultipleFieldValue(fieldNames);
            if(null==fieldValue){
                continue;
            }
            if(instanceQueryOption.distinct&&fieldValueList.contains(fieldValue)){
                continue;
            }
            fieldValueList.add(fieldValue);
        }
        flowContext.putReturnData("fieldValueList", fieldValueList);
    }

    @Override
    public String name() {
        return "获取实例列表多个字段值列表";
    }
}